<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>采购订单API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }

        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 6px;
            background: #fafafa;
        }

        .test-section h3 {
            color: #555;
            margin-top: 0;
        }

        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
            font-size: 14px;
        }

        button:hover {
            background: #0056b3;
        }

        button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }

        .result {
            margin-top: 15px;
            padding: 15px;
            border-radius: 4px;
            font-family: monospace;
            white-space: pre-wrap;
            max-height: 300px;
            overflow-y: auto;
        }

        .success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }

        .error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }

        .info {
            background: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }

        input,
        select {
            padding: 8px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 200px;
        }

        .form-group {
            margin: 10px 0;
        }

        label {
            display: inline-block;
            width: 120px;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <h1>采购订单API测试</h1>

    <div class="container">
        <h2>API连接状态</h2>
        <button onclick="testConnection()">测试连接</button>
        <div id="connectionResult" class="result"></div>
    </div>

    <div class="container">
        <h2>获取采购订单列表</h2>
        <div class="form-group">
            <label>页码:</label>
            <input type="number" id="pageIndex" value="1" min="1">
        </div>
        <div class="form-group">
            <label>页大小:</label>
            <input type="number" id="pageSize" value="10" min="1" max="100">
        </div>
        <div class="form-group">
            <label>搜索文本:</label>
            <input type="text" id="searchText" placeholder="输入搜索关键词">
        </div>
        <button onclick="getPurchaseOrderList()">获取列表</button>
        <div id="listResult" class="result"></div>
    </div>

    <div class="container">
        <h2>获取采购订单详情</h2>
        <div class="form-group">
            <label>订单ID:</label>
            <input type="number" id="orderId" value="1" min="1">
        </div>
        <button onclick="getPurchaseOrderDetail()">获取详情</button>
        <div id="detailResult" class="result"></div>
    </div>

    <div class="container">
        <h2>创建采购订单</h2>
        <div class="form-group">
            <label>订单主题:</label>
            <input type="text" id="orderSubject" value="测试采购订单" placeholder="输入订单主题">
        </div>
        <div class="form-group">
            <label>采购日期:</label>
            <input type="date" id="purchaseDate">
        </div>
        <div class="form-group">
            <label>供应商ID:</label>
            <input type="number" id="supplierId" value="1" min="1">
        </div>
        <div class="form-group">
            <label>采购部门ID:</label>
            <input type="number" id="purchasingDepartmentId" value="1" min="1">
        </div>
        <div class="form-group">
            <label>采购人员ID:</label>
            <input type="number" id="purchaserId" value="1" min="1">
        </div>
        <div class="form-group">
            <label>结算方式:</label>
            <select id="settlementMethod">
                <option value="Cash">现金</option>
                <option value="BankTransfer">银行转账</option>
                <option value="Check">支票</option>
                <option value="Credit">信用</option>
                <option value="Other">其他</option>
            </select>
        </div>
        <button onclick="createPurchaseOrder()">创建订单</button>
        <div id="createResult" class="result"></div>
    </div>

    <div class="container">
        <h2>删除采购订单</h2>
        <div class="form-group">
            <label>订单ID:</label>
            <input type="number" id="deleteOrderId" value="1" min="1">
        </div>
        <button onclick="deletePurchaseOrder()">删除订单</button>
        <div id="deleteResult" class="result"></div>
    </div>

    <script>
        // 设置默认日期为今天
        document.getElementById('purchaseDate').value = new Date().toISOString().split('T')[0];

        const API_BASE_URL = 'http://localhost:5000';

        function showResult(elementId, message, type = 'info') {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.className = `result ${type}`;
        }

        function showError(elementId, error) {
            showResult(elementId, `错误: ${error.message || error}`, 'error');
        }

        async function testConnection() {
            try {
                const response = await fetch(`${API_BASE_URL}/api/WeatherForecast`);
                if (response.ok) {
                    showResult('connectionResult', '✅ API连接成功！', 'success');
                } else {
                    showResult('connectionResult', `❌ API连接失败: ${response.status}`, 'error');
                }
            } catch (error) {
                showError('connectionResult', error);
            }
        }

        async function getPurchaseOrderList() {
            try {
                const pageIndex = document.getElementById('pageIndex').value;
                const pageSize = document.getElementById('pageSize').value;
                const searchText = document.getElementById('searchText').value;

                const params = new URLSearchParams({
                    PageIndex: pageIndex,
                    PageSize: pageSize
                });

                if (searchText) {
                    params.append('SearchText', searchText);
                }

                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder/list?${params}`);
                const data = await response.json();

                if (response.ok) {
                    showResult('listResult', `✅ 获取成功！\n\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showError('listResult', data);
                }
            } catch (error) {
                showError('listResult', error);
            }
        }

        async function getPurchaseOrderDetail() {
            try {
                const orderId = document.getElementById('orderId').value;
                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder/${orderId}`);
                const data = await response.json();

                if (response.ok) {
                    showResult('detailResult', `✅ 获取成功！\n\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showError('detailResult', data);
                }
            } catch (error) {
                showError('detailResult', error);
            }
        }

        async function createPurchaseOrder() {
            try {
                const orderData = {
                    orderSubject: document.getElementById('orderSubject').value,
                    purchaseDate: document.getElementById('purchaseDate').value,
                    supplierId: parseInt(document.getElementById('supplierId').value),
                    purchasingDepartmentId: parseInt(document.getElementById('purchasingDepartmentId').value),
                    purchaserId: parseInt(document.getElementById('purchaserId').value),
                    settlementMethod: document.getElementById('settlementMethod').value,
                    isSystemNumber: true,
                    doNotBindSalesOrder: false,
                    doNotBindPurchaseRequest: true,
                    details: [
                        {
                            serialNumber: 1,
                            itemNumber: "TEST001",
                            itemName: "测试物品",
                            specificationModel: "TEST-SPEC",
                            unit: "个",
                            brand: "测试品牌",
                            purchaseQuantity: 10,
                            mainUnitQuantity: 10,
                            taxRate: 13,
                            unitPriceIncludingTax: 100,
                            unitPriceExcludingTax: 88.5
                        }
                    ]
                };

                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(orderData)
                });

                const data = await response.json();

                if (response.ok) {
                    showResult('createResult', `✅ 创建成功！\n\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showError('createResult', data);
                }
            } catch (error) {
                showError('createResult', error);
            }
        }

        async function deletePurchaseOrder() {
            try {
                const orderId = document.getElementById('deleteOrderId').value;
                const response = await fetch(`${API_BASE_URL}/api/PurchaseOrder/${orderId}`, {
                    method: 'DELETE'
                });

                const data = await response.json();

                if (response.ok) {
                    showResult('deleteResult', `✅ 删除成功！\n\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showError('deleteResult', data);
                }
            } catch (error) {
                showError('deleteResult', error);
            }
        }

        // 页面加载完成后自动测试连接
        window.addEventListener('load', testConnection);
    </script>
</body>

</html>